package com.yixing.tech.wcs.controller;

import com.yixing.tech.common.autoline.constant.ResultStatus;
import com.yixing.tech.common.autoline.domain.*;
import com.yixing.tech.wcs.service.ICallbackService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * WCS堆垛机回调接口服务
 * */
@RestController
@CrossOrigin
@RequestMapping(value = "/api/WCS")
@Slf4j
public class WCSCallbackController {

    @Resource
    ICallbackService callbackService;

    /** 1.WCS通知WMS任务结束 */
    @PostMapping("/InstructionFinish")
    public WCSResponse taskFinishCallback(@RequestBody List<TaskFinishReq> taskFinishReqList) {
        log.info("WCS通知堆垛机任务完成:请求数据[{}]", taskFinishReqList);

        Object obj = callbackService.wcsTaskFinish(taskFinishReqList);

        WCSResponse response = new WCSResponse();
        response.setRESULT(ResultStatus.SUCCESS);
        response.setHIGJTSIGNAL(1);
        response.setMESSAGE("");
        response.setDATA(obj);
        log.info("WCS通知堆垛机任务完成:响应数据[{}]", response);
        return response;
    }

    /** 2.WCS获取新的任务货位 */
    @PostMapping("/GetNewLocation")
    public WCSResponse newLocationGet(@RequestBody GetNewLocationReq getNewLocationReq) {
        log.info("WCS获取新的任务货位:请求参数[{}]", getNewLocationReq);

        Object obj = callbackService.newLocationGet(getNewLocationReq);
        WCSResponse response = new WCSResponse();
        response.setRESULT(ResultStatus.SUCCESS);
        response.setHIGJTSIGNAL(getNewLocationReq.getSIGNAL_HEIGHT());
        response.setDATA(obj);
        log.info("WCS获取新的任务货位:响应数据[{}]", response);
        return response;
    }

    /** 3.WCS取消WMS下发的任务 */
    @PostMapping("/CancelInstructions")
    public WCSResponse taskCancelCallback(@RequestBody List<TaskCancelReq> taskCancelReqList) {
        log.info("WCS取消WMS下发的任务:请求参数[{}]", taskCancelReqList);

        Object obj = callbackService.wcsTaskCancel(taskCancelReqList);

        WCSResponse response = new WCSResponse();
        response.setRESULT(ResultStatus.SUCCESS);
        response.setDATA(obj);
        log.info("WCS取消WMS下发的任务:响应数据[{}]", response);
        return response;
    }

    /** 4.WCS故障上报 */
    @PostMapping("/ReportErrorInfo")
    public WCSResponse taskErrorCallback(@RequestBody TaskErrorReq taskErrorReq) {
        log.info("WCS故障上报:请求参数[{}]", taskErrorReq);

        Object data = callbackService.wcsTaskError(taskErrorReq);
        WCSResponse response = new WCSResponse();

        response.setRESULT(ObjectUtils.isNotEmpty(data) ? ResultStatus.SUCCESS: ResultStatus.FAIL);
        response.setHIGJTSIGNAL(1);
        response.setDATA("NULL".equals(data) ? "": data);
        return response;
    }

    /** 5.WCS通知AGV完成（打开光栅/关闭光栅） */
    @PostMapping("/InformFinish")
    public WCSResponse agvRasterReqResult(@RequestBody AgvRasterResult agvRasterResult) {
        log.info("WCS通知AGV完成光栅操作:请求参数[{}]", agvRasterResult);

        callbackService.wcsAgvRasterReqFinish(agvRasterResult);
        WCSResponse response = new WCSResponse();

        response.setRESULT(ResultStatus.SUCCESS);
        response.setHIGJTSIGNAL(1);
        return response;
    }

}
